/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode dummy{0, head};
        ListNode *left=&dummy, *right=&dummy;
        while(n--) right=right->next;
        while(right->next) left=left->next, right=right->next;
        ListNode *nxt=left->next;
        left->next=left->next->next;
        delete nxt;
        return dummy.next;
    }
};